package com.inibb.app.listener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.web.context.support.WebApplicationContextUtils;

import com.inibb.app.constant.GlobalVariables;
import com.inibb.app.global.I18nMessagesMapBean;
import com.inibb.app.global.SysConfigBean;
import com.inibb.app.service.admin.IGenericCodeService;
import com.inibb.app.service.admin.II18nMsgService;
import com.inibb.app.service.admin.ISaSystemParamService;
import com.inibb.app.util.PropertyUtils;
import com.inibb.app.vo.GenericCodeVO;
import com.inibb.app.vo.admin.SaSystemParamVO;


public class GlobalVariablesistener implements ServletContextListener {

	public void contextDestroyed(ServletContextEvent sce) {
		ServletContext ctx = sce.getServletContext();
		ctx.removeAttribute("global");
		ctx.removeAttribute("locales");
	}

	public void contextInitialized(ServletContextEvent sce) {
		ServletContext ctx = sce.getServletContext();
		ctx.setAttribute("global", new GlobalVariables());
		
		SysConfigBean sysConfig = (SysConfigBean) getBean(ctx,SysConfigBean.class);
		sysConfig.load(ctx);
		//ctx.setAttribute("sysConfigMap", sysConfig.getSysConfigMap());
		
//		String iniAppSiteName =  (String) PropertyUtils.getContextProperty("iniapp.site.name");
//		ctx.setAttribute("iniAppSiteName", iniAppSiteName);
//		String userDir = System.getProperty("user.dir");
//		ctx.setAttribute("userDir", userDir);
		
		/*Locale[] SUPPORTED_LOCALES = new Locale[3];
		SUPPORTED_LOCALES[0] = Locale.US;
		SUPPORTED_LOCALES[1] = Locale.SIMPLIFIED_CHINESE;
		SUPPORTED_LOCALES[2] = new Locale("zh", "HK");
		ctx.setAttribute("locales", SUPPORTED_LOCALES);*/
//		String[] codeTypes = new String[5];
//		codeTypes[0] = GenericCodeConstant.CODE_TYPE_GOODS_TYPE;
//		codeTypes[1] = GenericCodeConstant.CODE_TYPE_INFO;
//		codeTypes[2] = GenericCodeConstant.CODE_TYPE_LOCATION;
//		codeTypes[3] = GenericCodeConstant.CODE_TYPE_SHOP_TYPE;
//		codeTypes[4] = GenericCodeConstant.CODE_TYPE_USER_INFO;
//		ctx.setAttribute("codeTypes", genericCodeService.searchCodeType());
//		II18nMsgService i18nMsgService = (II18nMsgService) getBean(ctx,II18nMsgService.class);
//		I18nMessagesMapBean i18nMessagesMap = new I18nMessagesMapBean();
//		i18nMessagesMap.init(i18nMsgService);
//		ctx.setAttribute("i18nMessagesMap", i18nMessagesMap);
//		IGenericCodeService genericCodeService = (IGenericCodeService) getBean(ctx,IGenericCodeService.class);
//		List<GenericCodeVO> codeTypes = genericCodeService.searchCodeType();
//		ctx.setAttribute("codeTypes", codeTypes);
//		
//		//get system parameter
//		ISaSystemParamService systemParamService = (ISaSystemParamService) getBean(ctx,ISaSystemParamService.class);
//		List<SaSystemParamVO> systemParams = systemParamService.searchSystemParam();
//		Map<String, SaSystemParamVO> systemParamMap = new HashMap<String, SaSystemParamVO>();
//		for(SaSystemParamVO v:systemParams){
//			systemParamMap.put(v.getName(), v);
//		}
//		ctx.setAttribute("systemParamMap", systemParamMap);
//		
//		//GET product type
//		List<GenericCodeVO> pskyProductTypes = genericCodeService.searchCodeByParentId(100007);//100009
//		ctx.setAttribute("pskyProductTypes", pskyProductTypes);
	}
	
	private Object getBean(ServletContext ctx,Class t){
		return WebApplicationContextUtils.getWebApplicationContext(ctx).getBean(t);
	}

}
